/********************************** (C) COPYRIGHT *******************************
 * File Name          : CH392UART.C
 * Author             : WCH
 * Version            : V1.1
 * Date               : 2022/12/27
 * Description        : Hardware abstraction layer for CH392 UART V1.1
 *******************************************************************************/
#include "includes.h"
#include "CH392UART.h"

#include "CH392INC.H"

#include "bsp.h"

#define UART_INIT_BAUDRATE 9600 /*The default baudrate of communication is 9600bps, but higher baud rate can be set by command */

/*******************************************************************************
 * Function Name  : CH392_Port_Init
 * Description    : Initialize the UART Port
 * Input          : None
 * Return         : None
 *******************************************************************************/
void CH392_UARTPort_Init(void)
{
//	GPIO_InitTypeDef GPIO_InitStructure;
//	USART_InitTypeDef USART_InitStructure;

//	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//	GPIO_Init(GPIOA, &GPIO_InitStructure);
//	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
//	GPIO_Init(GPIOA, &GPIO_InitStructure);

//	USART_InitStructure.USART_BaudRate = UART_INIT_BAUDRATE;
//	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
//	USART_InitStructure.USART_StopBits = USART_StopBits_1;
//	USART_InitStructure.USART_Parity = USART_Parity_No;
//	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;

//	USART_Init(USART2, &USART_InitStructure);
//	USART_Cmd(USART2, ENABLE);
}

/*******************************************************************************
 * Function Name  : Set_MCU_BaudRate
 * Description    : Set baudrate
 * Input          : u32 BAUDRATE
 * Return         : None
 *******************************************************************************/
void Set_MCU_BaudRate(u32 BAUDRATE)
{
//	USART_InitTypeDef USART_InitStructure;

//	USART_InitStructure.USART_BaudRate = BAUDRATE;
//	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
//	USART_InitStructure.USART_StopBits = USART_StopBits_1;
//	USART_InitStructure.USART_Parity = USART_Parity_No;
//	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;

//	USART_Init(USART2, &USART_InitStructure);
	
	usartInitialize(USART0,BAUDRATE);
}

/*******************************************************************************
 * Function Name  : USART2_SendByte
 * Description    : The USART2 sends one byte
 * Input          : u8 Data
 * Return         : None
 *******************************************************************************/
void USART2_SendByte(u8 Data)
{
//	USART_SendData(USART2, Data);
//	while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
//	{
//	}
	
	vUsartSendByte(USART0, Data);
}

/*******************************************************************************
 * Function Name  : USART2_RcvByte
 * Description    : The USART2 receive one byte
 * Input          : None
 * Return         : u8 i
 *******************************************************************************/
u8 USART2_RcvByte(void)
{
//	u8 i;
//	while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
//	{
//	}
//	i = USART_ReceiveData(USART2);
//	return i;
	
	return xUsartRecByte(USART0);
}

/********************************************************************************
 * Function Name  : UARTWriteCH392Cmd
 * Description    : Write CH392 Cmd
 * Input          : u8 cmd
 * Output         : None
 * Return         : None
 *******************************************************************************/
void UARTWriteCH392Cmd(u8 cmd)
{
	USART2_SendByte(SER_SYNC_CODE1); /* Start operation of the first serial port synchronization code */
	USART2_SendByte(SER_SYNC_CODE2); /* Start operation of the second serial port synchronization code*/
	USART2_SendByte(cmd);			 /* cmd */
}

/********************************************************************************
 * Function Name  : UARTWriteCH392Data
 * Description    : Write CH392 Data
 * Input          : u8 mdata
 * Output         : None
 * Return         : None
 *******************************************************************************/
void UARTWriteCH392Data(u8 mdata)
{
	USART2_SendByte(mdata); /* data */
}

/********************************************************************************
 * Function Name  : UARTReadCH392Data
 * Description    : Read CH392 Data
 * Input          : None
 * Output         : None
 * Return         : u8 i
 *******************************************************************************/
u8 UARTReadCH392Data(void)
{
	u8 i;
	i = USART2_RcvByte();
	return i;
}
